﻿//Created by Action Script Viewer - http://www.buraks.com/asv
package Classes.effects {
    import Engine.Classes.*;
    import Engine.Events.*;
    import flash.display.*;
    import Engine.Helpers.*;
    import Classes.*;
    import com.greensock.*;
    import com.greensock.easing.*;

    public class CoinPickEffect extends MapResourceEffect {

        public static const PICK_0:String = "";
        public static const PICK_1:String = "coinPick1";
        public static const PICK_2:String = "coinPick2";
        public static const PICK_3:String = "coinPick3";
        public static const COIN_SPIN_1:String = "coinSpin1";
        public static const COIN_SPIN_1_DOUBLE:String = "coinSpin1double";

        private const EFFECT_NAME:String = "coinPick";
        private const PICK_4:String = "coinPick4";
        private const BOUNCE_HEIGHT:Number = 75;

        private var m_pickSizeOrder:Array;
        private var m_isComplete:Boolean = false;
        private var m_isLoading:Boolean = false;
        private var m_isItemImagesLoaded:Boolean = false;
        private var m_itemImageInstances:Array;
        private var m_itemImageLoadedHash:Object;
        private var m_currentPickName:String = "";
        private var m_targetPickName:String = "";
        private var m_coinSpinName:String = "coinSpin1";
        private var m_pickContainer:Sprite;
        private var m_currentPickImage:DisplayObject;
        private var m_currentBounceImage:AnimatedBitmap;
        private var m_animTimeline:TimelineLite;
        private var m_effectX:Number;
        private var m_effectY:Number;
        private var m_pickOffsetX:Number;
        private var m_pickOffsetY:Number;

        public function CoinPickEffect(_arg1:MapResource){
            this.m_pickSizeOrder = [PICK_1, PICK_2, PICK_3, this.PICK_4];
            super(_arg1);
            this.animate(1);
            this.m_pickOffsetX = 0;
            this.m_pickOffsetY = 0;
        }
        override public function animate(_arg1:uint):Boolean{
            if (this.m_isComplete){
                return (false);
            };
            if (((isMapResourceLoaded) && (!(this.m_isLoading)))){
                this.loadAnimationEffect();
            } else {
                if (this.m_currentBounceImage){
                    this.m_currentBounceImage.onUpdate((1 / 15));
                };
            };
            return (true);
        }
        override public function cleanUp():void{
            if (this.m_animTimeline){
                this.m_animTimeline.kill();
            };
            if (this.m_currentPickImage){
                this.removeFromParent(this.m_currentPickImage);
                this.m_currentPickImage = null;
            };
            if (this.m_currentBounceImage){
                this.removeFromParent(this.m_currentBounceImage);
                this.m_currentBounceImage = null;
            };
            this.m_itemImageLoadedHash = null;
            this.m_isComplete = true;
        }
        public function setPickType(_arg1:String):void{
            if (!this.m_currentPickName){
                this.m_currentPickName = _arg1;
            };
            if (!this.m_currentPickImage){
                this.m_currentPickName = _arg1;
                this.reattach();
            } else {
                if (this.m_targetPickName != _arg1){
                    this.m_targetPickName = _arg1;
                    this.startBubble();
                };
            };
        }
        public function set pickOffsetX(_arg1:Number):void{
            this.m_pickOffsetX = _arg1;
        }
        public function set pickOffsetY(_arg1:Number):void{
            this.m_pickOffsetY = _arg1;
        }
        public function setBounceType(_arg1:String):void{
            this.m_coinSpinName = _arg1;
        }
        public function bounceToType(_arg1:String=""):void{
            this.m_targetPickName = _arg1;
            if (((((this.m_isItemImagesLoaded) && (isMapResourceLoaded))) && (!(this.m_animTimeline)))){
                this.startBounce();
            };
        }
        override public function reattach():void{
            if (((!(this.m_isItemImagesLoaded)) || (!(isMapResourceLoaded)))){
                return;
            };
            var _local1:Vector3 = m_mapResource.getReference().getSize();
            var _local2:IsoRect = IsoRect.getIsoRectFromSize(_local1);
            var _local3:Number = m_mapResource.displayObjectOffsetX;
            var _local4:Number = (-(m_mapResource.displayObjectOffsetY) / m_mapResource.displayObject.scaleY);
            var _local5:Number = ((_local4 - _local2.height) + (_local2.height - _local2.bottom.y));
            this.m_effectX = ((_local3 + (_local2.width >> 1)) + this.m_pickOffsetX);
            this.m_effectY = ((_local5 - _local2.height) + this.m_pickOffsetY);
            if (!this.m_pickContainer){
                this.m_pickContainer = new Sprite();
            };
            var _local6:Sprite = this.getAttachParent();
            if (((!(this.m_pickContainer.parent)) || (((this.m_pickContainer.parent) && (!((this.m_pickContainer.parent == _local6))))))){
                _local6.addChild(this.m_pickContainer);
            };
            this.setPickImage(this.m_currentPickName);
            if (this.m_currentPickImage){
                this.m_currentPickImage.visible = false;
            };
        }
        private function setPickImage(_arg1:String):void{
            this.removeFromParent(this.m_currentPickImage);
            if (((_arg1) && (this.m_pickContainer))){
                this.m_currentPickImage = (this.getImage(_arg1) as DisplayObject);
                this.m_currentPickImage.x = (-(this.m_currentPickImage.width) >> 1);
                this.m_currentPickImage.y = (-(this.m_currentPickImage.height) >> 1);
                this.m_pickContainer.addChild(this.m_currentPickImage);
                this.m_pickContainer.x = this.m_effectX;
                this.m_pickContainer.y = this.m_effectY;
                this.m_pickContainer.width = this.m_currentPickImage.width;
                this.m_pickContainer.height = this.m_currentPickImage.height;
            };
        }
        private function startBounce():void{
            this.removeFromParent(this.m_currentBounceImage);
            this.m_currentBounceImage = (this.getImage(this.m_coinSpinName) as AnimatedBitmap);
            var _local1:Sprite = this.getAttachParent();
            var _local2 = -1;
            if (this.m_pickContainer){
                if (!this.m_pickContainer.parent){
                    this.reattach();
                };
                _local2 = _local1.getChildIndex(this.m_pickContainer);
            };
            if (_local2 < 0){
                return;
            };
            _local1.addChildAt(this.m_currentBounceImage, _local2);
            this.m_currentBounceImage.x = (this.m_effectX - (this.m_currentBounceImage.width >> 1));
            this.m_currentBounceImage.y = (this.m_effectY - (this.m_currentBounceImage.height >> 1));
            this.m_animTimeline = new TimelineLite({onComplete:this.bounceCompleteHandler});
            this.m_animTimeline.appendMultiple([new TweenLite(this.m_currentBounceImage, 0.3, {
                y:(this.m_currentBounceImage.y - this.BOUNCE_HEIGHT),
                ease:Quad.easeOut
            }), new TweenLite(this.m_currentBounceImage, 0.3, {
                y:this.m_currentBounceImage.y,
                ease:Quad.easeIn
            })], 0, TweenAlign.SEQUENCE);
        }
        private function bounceCompleteHandler():void{
            this.m_animTimeline = null;
            this.removeFromParent(this.m_currentBounceImage);
            if (this.m_currentPickImage){
                this.m_currentPickImage.visible = true;
                this.startBubble();
            };
        }
        private function startBubble():void{
            if (this.m_animTimeline){
                return;
            };
            if (this.m_targetPickName == ""){
                this.m_targetPickName = ((this.m_currentPickName) || (PICK_1));
            };
            var _local1:Number = this.m_pickContainer.width;
            var _local2:Number = this.m_pickContainer.height;
            var _local3:DisplayObject = (this.getImage(this.m_targetPickName) as DisplayObject);
            var _local4:String = this.getNextSizePickName(this.m_targetPickName);
            this.setPickImage(_local4);
            this.m_pickContainer.width = _local1;
            this.m_pickContainer.height = _local2;
            this.m_animTimeline = new TimelineLite({onComplete:this.bubbleCompleteHandler});
            this.m_animTimeline.appendMultiple([new TweenLite(this.m_pickContainer, 0.2, {
                width:this.m_currentPickImage.width,
                height:this.m_currentPickImage.height,
                ease:Quad.easeOut
            }), new TweenLite(this.m_pickContainer, 0.2, {
                width:_local3.width,
                height:_local3.height,
                ease:Quad.easeIn
            })], 0, TweenAlign.SEQUENCE);
        }
        private function bubbleCompleteHandler():void{
            this.m_animTimeline = null;
            this.setPickImage(this.m_targetPickName);
            this.m_currentPickImage.visible = false;
        }
        private function removeFromParent(_arg1:DisplayObject):void{
            if (((_arg1) && (_arg1.parent))){
                _arg1.parent.removeChild(_arg1);
            };
        }
        private function getImage(_arg1:String):Object{
            var _local2:ItemImageInstance = this.m_itemImageLoadedHash[_arg1];
            var _local3:Object = _local2.image;
            return (_local3);
        }
        private function getAttachParent():Sprite{
            var _local1:Sprite = (m_mapResource.getDisplayObject() as Sprite);
            return (_local1);
        }
        private function getNextSizePickName(_arg1:String):String{
            var _local2:int = this.m_pickSizeOrder.indexOf(_arg1);
            var _local3:String = this.m_pickSizeOrder[(_local2 + 1)];
            return (_local3);
        }
        private function loadAnimationEffect():void{
            var _local3:XML;
            var _local4:ItemImage;
            var _local5:String;
            var _local1:XML = Global.gameSettings.getEffectByName(this.EFFECT_NAME);
            var _local2:XMLList = _local1.image;
            this.m_itemImageLoadedHash = {};
            for each (_local3 in _local2) {
                _local5 = ((_local3.attribute("name")) || (this.m_coinSpinName));
                this.m_itemImageLoadedHash[_local5] = null;
            };
            for each (_local3 in _local2) {
                _local4 = new ItemImage(_local3);
                _local4.addEventListener(LoaderEvent.LOADED, this.onItemImageLoaded, false, 0, true);
                _local4.load();
            };
            this.m_isLoading = true;
        }
        private function onItemImageLoaded(_arg1:LoaderEvent):void{
            var _local2:ItemImage;
            var _local3:ItemImageInstance;
            var _local4:String;
            var _local5:Boolean;
            if (!this.m_isItemImagesLoaded){
                _local2 = (_arg1.target as ItemImage);
                _local2.removeEventListener(LoaderEvent.LOADED, this.onItemImageLoaded);
                _local3 = _local2.getInstance();
                if (!_local3){
                    return;
                };
                _local4 = ((_local2.name) || (this.m_coinSpinName));
                if (this.m_itemImageLoadedHash){
                    this.m_itemImageLoadedHash[_local4] = _local3;
                    _local5 = true;
                    for each (_local3 in this.m_itemImageLoadedHash) {
                        if (!_local3){
                            _local5 = false;
                            break;
                        };
                    };
                };
                if (_local5){
                    this.m_isItemImagesLoaded = true;
                    this.reattach();
                };
            };
        }

    }
}//package Classes.effects 
